<?php
/*
 * @Author: zwx
 * @Date: 2024-05-30 12:05:58
 * @LastEditors: zwx
 * @LastEditTime: 2024-08-18 15:09:52
 * @Description: 
 */
declare(strict_types=1);

namespace app\apiadmin\command;

use app\apiadmin\controller\behavior\Invitation;
use app\apiadmin\controller\liveManager\LiveStatus;
use app\apiadmin\model\action\ActionModel;
use app\apiadmin\service\invitation\InvitationService;
use app\apiadmin\service\liveManager\LiveManagerService;
use app\apiadmin\service\TCSS\TImService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;
use think\facade\Log;

class shuashu_signin extends Command
{

    protected function configure()
    {
        // 命令的名称及用途描述
        $this->setName('shuashu_signin')->setDescription('邀约到课');
    }

    protected function execute(Input $input, Output $output)
    {
        // 你的后台运行代码
        $output->writeln("shuashu_signin");
        // 这里可以放置你的长时间运行的代码

        self::shuashu();

        // $ls = new LiveStatus(1);
        // $liveId = $ls->set_dabiaoqian_live(200);

    }




    public function shuashu()
    {
        $liveId = 355;

        //查询用户信息
        $findName = [];
        $map = [];
        $map['del'] = "0";
        $collection = 'live_user_' . $liveId;
        $result = ActionModel::live_user_info($collection, $map, $findName);

        foreach ($result as $k => $v) {
            // Log::channel("invitation")->info("shuashu-signin-setGongyuOrderState:" . $liveId . ' --- ' . $v['live_user_id'] . ' - ' . date('Y-m-d H:i:s'));
            file_put_contents("./invitation.txt", "shuashu-signin-setGongyuOrderState:" . $liveId . ' --- ' . $v['live_user_id'] . ' - ' . date('Y-m-d H:i:s') . "\n", FILE_APPEND);
            // $this->setGongyuOrderState($liveId, $v['live_user_id']);
        }
    }



    /**
     * 进入直播间 修改订单到课状态
     * @return \think\response\Json
     * @throws \Exception
     * @author cc
     */
    public function setGongyuOrderState($live_id, $live_user_id)
    {
        Log::channel("invitation")->info("signin-setGongyuOrderState:" . $live_id . ' --- ' . $live_user_id . ' - ' . date('Y-m-d H:i:s'));
        //打日志，做记录
        $m = [];
        $m['liveId'] = $live_id;
        $m['live_user_id'] = $live_user_id;  //用户ID

        $s = db::table('xh_siyu_invitation_user_signin')->where($m)->find();
        if (!$s) {
            $a = [];
            $a['xh_siyu_user.id'] = $m['live_user_id'];
            $a['xh_siyu_invitation_user.liveId'] = $m['liveId'];
            $b = db::table('xh_siyu_invitation_user')
                ->join('xh_siyu_user', 'xh_siyu_invitation_user.wx_unionid = xh_siyu_user.unionid', 'inner')
                ->where($a)->find();

            //到课
            $c = [];
            $c['liveId'] = $live_id;
            $c['live_user_id'] = $live_user_id;
            $c['signin'] = 0;
            $c['signin_time'] = date('Y-m-d H:i:s');
            if ($b) {
                $c['signin'] = 2;
                $c['signin_time'] = date('Y-m-d H:i:s');
            }

            db::table('xh_siyu_invitation_user_signin')->insertGetId($c);
        }

        // $this->validate($param, OrderStateValidate::class);
        $session = Db::table('xh_siyu_live_manager')->where('id', $live_id)->value('venue');
        //        $unionId = Db::table('xh_siyu_user')->where('id', $live_user_id)->value('unionid'); //$live_user_id   为  xh_siyu_user表的助教ID

        $unionId = Db::table('xh_siyu_user_order_zhujiao_log')->where(['live_id' => $live_id, 'user_id' => $live_user_id])->field('user_union_id,xh_order_union_id')->find();
        Log::channel("liveInfo")->info("zhujiao_log:" . $live_id . ' - ' . $live_user_id . ' - ' . json_encode($unionId) . ' - ' . date('Y-m-d H:i:s'));

        $unionIds = [];
        if (empty($unionId)) {  //非空判断
            echo 'sucess';
            return false;
        }

        $unionIds = array_values($unionId);

        $where2 = [];
        $where2[] = ['liveId', '=', $live_id];
        $where2[] = ['venue', '=', $session];
        $where2[] = ['wx_unionid', 'IN', $unionIds];
        $user2 = Db::table('xh_siyu_invitation_user')->where($where2)->find();
        if ($user2) {   //查询不到不打到课
            $where = [];
            $where[] = ['liveId', '=', $live_id];
            $where[] = ['venue', '=', $session];
            $where[] = ['wx_unionid', 'IN', $unionIds];
            Db::table('xh_siyu_invitation_user')->where($where)->update([
                'signin_status' => 2,
                'signin_time' => date('Y-m-d H:i:s'),
            ]);
        }


        $where = [];
        $where[] = ['wx_union_id', 'IN', $unionIds];
        $where[] = ['pay_state', '=', 1];
        $where[] = ['del', '=', 0];
        $where[] = ['is_association_siyu', '=', 0];
        $where[] = ['pay_time', '<', date('Y-m-d H:i:s')];
        $where[] = ['pingtai', 'IN', [1, 2, 4, 6]];
        $orderArray = Db::table('xh_order_1')->where($where)->column('id,wx_union_id');
        if (!empty($orderArray)) {
            foreach ($orderArray as $value) {
                if (!empty($unionId)) {
                    echo json_encode($value);
                    $this->getLiveOrderState($live_id, $session, $value['id'], 2, $value['wx_union_id']);
                }
            }
        }
        echo 'sucess';
        return;
    }


    /**
     * 直播订单状态更改
     * @param $liveId '直播间id'
     * @param $session '场次'
     * @param $orderId '公域订单id'
     * @param $type '当前状态 0：无、1:邀约、2:到课、3:成交'
     * @param $unionId '用户union_id'
     * @param $tutorId '邀请人id'
     * @param $siyuId '私域订单id'
     * @param $money '成交金额'
     * @return bool|string
     * @author cc
     */
    public function getLiveOrderState($liveId, $session, $orderId, $type, $unionId = '', $tutorId = '', $siyuId = '', $money = '')
    {
        try {
            $orderInfo = Db::table('xh_order_1')->where('id', $orderId)->find();
            if (!$orderInfo) {
                throw new \Exception("订单不存在!");
            }
            $date = date('Y-m-d H:i:s');
            $data = $changes = [];
            $yaoyue1 = $yaoyue2 = $daoke1 = $daoke2 = 0;
            switch ($type) {
                case 1:
                    $data = [
                        'new_yaoyue_tag' => 1,
                        'new_yaoyue_tag_time' => $date,
                    ];
                    if ($session == 1) {
                        $yaoyue1 = 1;
                        $changes = [
                            'yaoyue_day1' => 1,
                        ];
                    }
                    if ($session == 2) {
                        $yaoyue2 = 1;
                        $changes = [
                            'yaoyue_day2' => 1,
                        ];
                    }
                    break;
                case 2:
                    $data = [
                        'new_daoke_tag' => 1,
                        'new_daoke_tag_time' => $date,
                    ];
                    if ($session == 1) {
                        $yaoyue1 = 1;
                        $daoke1 = 1;
                        $changes = [
                            'yaoyue_day1' => 1,
                            'daoke_day1' => 1,
                        ];
                    }
                    if ($session == 2) {
                        $yaoyue2 = 1;
                        $daoke2 = 1;
                        $changes = [
                            'yaoyue_day2' => 1,
                            'daoke_day2' => 1,
                        ];
                    }
                    break;
                case 3:
                    $data = [
                        'new_chengjiao_tag' => 1,
                        'new_chengjiao_tag_time' => $date,
                    ];
                    if ($session == 1) {
                        $yaoyue1 = 1;
                        $daoke1 = 1;
                        $changes = [
                            'yaoyue_day1' => 1,
                            'daoke_day1' => 1,
                        ];
                    }
                    if ($session == 2) {
                        $yaoyue2 = 1;
                        $daoke2 = 1;
                        $changes = [
                            'yaoyue_day2' => 1,
                            'daoke_day2' => 1,
                        ];
                    }
                    break;
                default:
                    break;
            }

            if ($type != 0 && !empty($data)) {
                // 修改 order_1 订单状态
                Db::table('xh_order_1')->where('id', $orderId)->update($data);

                $changesInfo = Db::table('xh_order_changes')->where('order_id', $orderId)->find();
                if (!$changesInfo) {
                    Db::table('xh_order_changes')->insert([
                        'order_id' => $orderId,
                        'yaoyue_day1' => $yaoyue1,
                        'yaoyue_day2' => $yaoyue2,
                        'daoke_day1' => $daoke1,
                        'daoke_day2' => $daoke2,
                        'time' => date('Y-m-d H:i:s'),
                    ]);
                } else {
                    Db::table('xh_order_changes')->where('id', $changesInfo['id'])->update($changes);
                }
            }

            //            if ($type == 2) {
//                Log::channel("liveOrderInfo")->info("liveOrderInfo:daoke：" . $unionId . ' - ' . $date);
//                if (!empty($unionId)) {
//                    $where = [];
//                    $where[] = ['liveId', '=', $liveId];
//                    $where[] = ['venue', '=', $session];
//                    $where[] = ['wx_unionid', '=', $unionId];
//                    Db::table('xh_siyu_invitation_user')->where($where)->update([
//                        'signin_status' => 2,
//                        'signin_time' => $date,
//                    ]);
//                }
//            }
            if ($type == 3) {
                Log::channel("liveOrderInfo")->info("liveOrderInfo:chengjiao：" . $unionId . ' - ' . $siyuId . ' - ' . $date);
                Db::table('xh_order_1')->where('id', $orderId)->update(['is_association_siyu' => 1]);
                if (!empty($siyuId) && !empty($tutorId) && !empty($unionId)) {
                    $userName = Db::table('xh_siyu_sys_member')->where('uid', $tutorId)->value('user_name');
                    $where = [];
                    $where[] = ['liveId', '=', $liveId];
                    $where[] = ['venue', '=', $session];
                    $where[] = ['wx_unionid', '=', $unionId];
                    $where[] = ['sender', '=', $userName];
                    $a = Db::table('xh_siyu_invitation_user')->where($where)->update([
                        'chengjiao_status' => 2,
                        'chengjiao_time' => $date,
                        'chengjiao_money' => $money,
                        'gongyu_order_id' => $orderId,
                        'siyu_order_id' => $siyuId,
                    ]);
                    $d['remark'] = date('Y-m-d H:i:s') . '--Db::table(xh_siyu_invitation_user)->3-- ' . $a . '-----' . json_encode($where);
                    DB::table('xh_siyu_user_remark')->save($d);
                }
            }
            // 新增/修改 直播订单状态
            $orderLiveDetail = Db::table('xh_order_live_detail')->where(['order_id' => $orderInfo['id'], 'live_id' => $liveId, 'session' => $session])->find();
            if (!$orderLiveDetail) {
                if ($type == 1) {
                    $data = [
                        'order_id' => $orderInfo['id'],
                        'live_id' => $liveId,
                        'session' => $session,
                        'type' => $type,
                        'del' => 0,
                        'time' => $date,
                    ];
                    Db::table('xh_order_live_detail')->insert($data);
                }
            } else {
                Db::table('xh_order_live_detail')->where('id', $orderLiveDetail['id'])->update(['type' => $type]);
            }

            return true;
        } catch (\PDOException $e) {
            Log::channel("liveOrderInfo")->error("add:liveOrderInfo:error:" . $e->getMessage());
            throw $e;
        }
    }


}